﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using GauSmart.core;

namespace GauSmart
{
    public partial class WeightForm : Form
    {
        private readonly Criteria _criteria;
        private List<TextBox> _txtWeightList;
        private List<Weight> _weightList;

        public List<Weight> WeightList
        {
            get
            {
                return _weightList;
            }
        }

        public WeightForm(Criteria criteria)
        {
            _criteria = criteria;
            _txtWeightList = new List<TextBox>();
            _weightList = new List<Weight>();
            InitializeComponent();
        }

        private void WeightForm_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < _criteria.suitabilities.Count; i++ )
            {
                Label lblWeight = new Label();
                TextBox txtWeight = new TextBox();

                lblWeight.Text = ((Suitability)_criteria.suitabilities[i]).name;
                lblWeight.Location = new Point(30, (i+1) * 40);
                lblWeight.AutoSize = true;

                txtWeight.Location = new Point(lblWeight.Width, lblWeight.Top - 3);

                _txtWeightList.Add(txtWeight);

                Controls.Add(lblWeight);
                Controls.Add(txtWeight);
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _weightList.Clear();
            for (int i = 0; i < _txtWeightList.Count; i++)
            {
                if (!string.IsNullOrEmpty(_txtWeightList[i].Text))
                {
                    _weightList.Add(new Weight
                                        {
                                            Name = ((Suitability)_criteria.suitabilities[i]).name,
                                            Value = double.Parse(_txtWeightList[i].Text)
                                        });
                }
                else
                {
                    MessageBox.Show("You should determine weight for all criterias.",
                        "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            Hide();
        }

        #region Disable Close Button
        //
        // http://www.codeproject.com/Articles/20379/Disabling-Close-Button-on-Forms
        //
        private const int CP_NOCLOSE_BUTTON = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams myCp = base.CreateParams;
                myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
                return myCp;
            }
        }
        #endregion
    }
}
